import { Navigate, createBrowserRouter } from 'react-router-dom'

/**
 * The above dynamic import cannot be analyzed by Vite.
 * See https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#limitations for supported dynamic import formats.
 */
// function lazyLoad(path) {
//   return async () => {
//     const page = await import(path)
//     return { Component: page.default }
//   }
// }

/**
 * 路由懒加载
 * https://reactrouter.com/en/main/route/lazy#lazy
 */

const router = createBrowserRouter([
  {
    path: '/',
    async lazy() {
      const MainLayout = await import('./layout/MainLayout')
      return { Component: MainLayout.default }
    },
    children: [
      // 首页
      // {
      //   path: '',
      //   async lazy() {
      //     const Dashboard = await import(/* webpackChunkName: "Dashboard" */ './pages/Dashboard')
      //     return { Component: Dashboard.default }
      //   }
      // },
      // 会话页
      {
        // path: '/chat',
        path: '',
        async lazy() {
          const SkillChatPage = await import(/* webpackChunkName: "SkillChatPage" */ './pages/ChatAppPage')
          return { Component: SkillChatPage.default }
        }
      },
      // 知识库
      {
        path: 'filelib',
        async lazy() {
          const FileLibPage = await import(/* webpackChunkName: "FileLibPage" */ './pages/FileLibPage')
          return { Component: FileLibPage.default }
        }
      },
      {
        path: 'filelib/:id',
        async lazy() {
          const FilesPage = await import(/* webpackChunkName: "FilesPage" */ './pages/FileLibPage/files')
          return { Component: FilesPage.default }
        }
      },
      {
        path: 'filelib/:id/:id',
        async lazy() {
          const KnowledgeDetails = await import(
            /* webpackChunkName: "KnowledgeDetails" */ './pages/FileLibPage/details'
          )
          return { Component: KnowledgeDetails.default }
        }
      },
      {
        path: 'filelib/create/:id?',
        async lazy() {
          const KnowledgeCreate = await import(/* webpackChunkName: "KnowledgeCreate" */ './pages/FileLibPage/create')
          return { Component: KnowledgeCreate.default }
        }
      },
      //插件
      {
        path: 'plugin',
        async lazy() {
          const PluginLayout = await import(/* webpackChunkName: "PluginLayout" */ './layout/PluginLayout')
          return { Component: PluginLayout.default }
        },
        children: [
          {
            path: '',
            async lazy() {
              const SkillToolsPage = await import(/* webpackChunkName: "SkillToolsPage" */ './pages/SkillPage/tabTools')
              return { Component: SkillToolsPage.default }
            }
          },
          {
            path: 'create/:id/',
            async lazy() {
              const EditToolPage = await import(/* webpackChunkName: "EditToolPage" */ './pages/SkillPage/EditToolPage')
              return { Component: EditToolPage.default }
            }
          }
        ]
      },
      // 提示词
      {
        path: 'callWord',
        async lazy() {
          const CallWordPage = await import(/* webpackChunkName: "CallWordPage" */ './pages/CallWordPage')
          return { Component: CallWordPage.default }
        }
      },
      // 智能体
      {
        path: 'application',
        async lazy() {
          const ApplicationPage = await import(/* webpackChunkName: "ApplicationPage" */ './pages/ApplicationPage')
          return { Component: ApplicationPage.default }
        }
      },
      // 模型页面
      {
        path: 'model',
        async lazy() {
          const ModelPage = await import(/* webpackChunkName: "ModelPage" */ './pages/ModelPage')
          return { Component: ModelPage.default }
        }
      },
      // 用户管理
      {
        path: 'user',
        async lazy() {
          const UsersPage = await import(/* webpackChunkName: "UsersPage" */ './pages/SystemPage/components/Users')
          return { Component: UsersPage.default }
        }
      },
      // 角色管理
      {
        path: 'role',
        async lazy() {
          const RolesPage = await import(/* webpackChunkName: "RolesPage" */ './pages/SystemPage/components/Roles')
          return { Component: RolesPage.default }
        }
      },
      // 企业管理
      {
        path: 'firm',
        async lazy() {
          const FirmPage = await import(/* webpackChunkName: "FirmPage" */ './pages/SystemPage//components/Firm')
          return { Component: FirmPage.default }
        }
      },
      {
        path: 'deprecated_sys_config',
        async lazy() {
          const SystemConfig = await import(
            /* webpackChunkName: "SystemConfig" */ './pages/SystemPage/components/Config'
          )
          return { Component: SystemConfig.default }
        }
      },
      // 工作流列表页面
      {
        path: 'workflow',
        async lazy() {
          const TabSkills = await import(/* webpackChunkName: "TabSkills" */ './pages/workflow/flowList')
          return { Component: TabSkills.default }
        }
      },
      //测试页面
      {
        path: 'test',
        async lazy() {
          const TestPage = await import(/* webpackChunkName: "TestPage" */ './pages/TestPage/index')
          return { Component: TestPage.default }
        }
      }
    ]
  },
  // 智能体编辑
  {
    path: '/applicationEdit/:id/',
    children: [
      {
        path: '',
        async lazy() {
          const ApplicationEditPage = await import(
            /* webpackChunkName: "ApplicationEditPage" */ './pages/ApplicationEditPage'
          )
          return { Component: ApplicationEditPage.default }
        }
      }
    ]
  },
  // 智能体上线
  {
    path: '/applicationEdit/topLine/:id/',
    children: [
      {
        path: '',
        async lazy() {
          const ApplicationTopLine = await import(
            /* webpackChunkName: "ApplicationTopLine" */ './pages/ApplicationTopLine'
          )
          return { Component: ApplicationTopLine.default }
        }
      }
    ]
  },
  // // 工作流图配置页面
  {
    path: '/workflow/:id/',
    children: [
      {
        path: '',
        async lazy() {
          // const FlowPage = await import(/* webpackChunkName: "FlowPage" */ './pages/FlowPage')
          const FlowPage = await import(/* webpackChunkName: "FlowPage" */ './pages/workflow')
          return { Component: FlowPage.default }
        }
      }
    ]
  },
  // 使用指南
  {
    path: '/manual',
    children: [
      {
        path: '',
        async lazy() {
          const UserManualPage = await import(/* webpackChunkName: "UserManualPage" */ './pages/UserManual')
          return { Component: UserManualPage.default }
        }
      }
    ]
  },
  // 错误路由统一定位到首页
  { path: '*', element: <Navigate to="/" replace /> }
])

/**
 * 保留路由记录，方便后期打开对应功能
 */

// import MainLayout from './layout/MainLayout'
// import BuildLayout from './layout/BuildLayout'
// import SkillChatPage from './pages/ChatAppPage'
// import ChatShare from './pages/ChatAppPage/chatShare'
// import DiffFlowPage from './pages/DiffFlowPage'
// import FlowPage from './pages/FlowPage'
// import ModelPage from './pages/ModelPage'
// import Doc from './pages/ModelPage/doc'
// import Report from './pages/Report'
// import EditAssistantPage from './pages/SkillPage/editAssistant'
// import L2Edit from './pages/SkillPage/l2Edit'
// import SkillAssisPage from './pages/SkillPage/tabAssistant'
// import SkillsPage from './pages/SkillPage/tabSkills'
// import Templates from './pages/SkillPage/temps'
// import SystemPage from './pages/SystemPage'

// const ErrorHoc = ({ Comp }) => {
//   return (
//     <ErrorBoundary
//       onReset={() => (window.location.href = window.location.href)}
//       FallbackComponent={CrashErrorComponent}
//     >
//       <Comp />
//     </ErrorBoundary>
//   )
// }
// const router = createBrowserRouter([
//   {
//     path: '/',
//     element: <MainLayout />,
//     children: [
//       // { path: '', element: <SkillChatPage /> },
//       // 知识库
//       { path: 'filelib', element: <FileLibPage /> },
//       { path: 'filelib/:id', element: <FilesPage /> },
//       { path: 'filelib/:id/:id', element: <KnowledgeDetails /> },
//       { path: 'filelib/create/:id?', element: <KnowledgeCreate /> },
//       // {
//       //   path: 'build',
//       //   element: <BuildLayout />,
//       //   children: [
//       //     { path: 'assist', element: <SkillAssisPage /> },
//       //     { path: 'skills', element: <SkillsPage /> },
//       //     { path: 'tools', element: <SkillToolsPage /> },
//       //     { path: '', element: <Navigate to="assist" replace /> }
//       //   ]
//       // },
//       // { path: 'build/skill', element: <L2Edit /> },
//       // { path: 'build/skill/:id/:vid', element: <L2Edit /> },
//       // { path: 'build/temps', element: <Templates /> },
//       // 插件
//       {
//         path: 'plugin',
//         element: <PluginLayout />,
//         children: [
//           { path: 'index', element: <SkillToolsPage select={null} onSelect={() => {}}/> },
//           { path: 'create/:id/', element: <EditToolPage /> }
//         ]
//       },
//       // { path: 'model', element: <ModelPage /> },
//       // { path: 'sys', element: <SystemPage /> },
//       // 提示词
//       { path: 'callWord', element: <CallWordPage /> },
//       // 智能体
//       { path: 'application', element: <ApplicationPage /> }
//     ]
//   },
//   // { path: 'model/doc', element: <Doc /> },
//   // {
//   //   path: '/flow',
//   //   children: [{ path: '', element: <ErrorHoc Comp={FlowPage} /> }]
//   // },
//   // {
//   //   path: '/assistant/:id/',
//   //   children: [{ path: '', element: <EditAssistantPage /> }]
//   // },
//   // 智能体编辑
//   {
//     path: '/applicationEdit/:id/',
//     children: [{ path: '', element: <ApplicationEditPage /> }]
//   },
//   // 独立会话页
//   // { path: '/chat', element: <SkillChatPage /> },
//   // { path: '/chat/:id/', element: <ChatShare /> },
//   // { path: '/report/:id/', element: <Report /> },
//   // { path: '/diff/:id/:vid/:cid', element: <ErrorHoc Comp={DiffFlowPage} /> },
//   // { path: "/test", element: <Test /> },
//   { path: '*', element: <Navigate to="/application" replace /> }
// ])

export default router
